﻿//------------------------------------------------------------
// All Rights Reserved , Copyright (C) 2010 , Jirisoft , Ltd. 
//------------------------------------------------------------

using System;
using System.Data;

namespace DotNet.Business
{
    using DotNet.DbUtilities;
    using DotNet.Model;
    using DotNet.Utilities;
    using DotNet.Manager;

    /// <summary>
    /// BaseUserPermissionScopeManager
    /// 用户权限域
    /// 
    /// 修改纪录
    ///
    ///     2010.04.23 版本：1.1 JiRiGaLa Enabled 不允许为NULL的错误解决。
    ///     2008.05.24 版本：1.0 JiRiGaLa 创建主键。
    ///
    /// 版本：1.0
    /// 
    /// <author>
    ///		<name>JiRiGaLa</name>
    ///		<date>2008.05.24</date>
    /// </author>
    /// </summary>
    public class BaseUserPermissionScopeManager : BaseManager, IBaseManager
    {
        public BaseUserPermissionScopeManager()
        {
            base.CurrentTableName = BaseResourcePermissionScopeTable.TableName;
        }

        public BaseUserPermissionScopeManager(IDbHelper dbHelper) : this()
        {
            DbHelper = dbHelper;
        }

        public BaseUserPermissionScopeManager(BaseUserInfo userInfo) : this()
        {
            UserInfo = userInfo;
        }

        public BaseUserPermissionScopeManager(IDbHelper dbHelper, BaseUserInfo userInfo) : this(dbHelper)
        {
            UserInfo = userInfo;
        }


        /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        ////                                                                                                         ////
        ////                                            权限域管理部分                                             ////
        ////                                                                                                         ////
        /////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        #region public string[] GetOrganizeIds(string userId, string permissionItemId) 获取职员的组织机构权限域主键数组
        /// <summary>
        /// 获取职员的组织机构权限域主键数组
        /// </summary>
        /// <param name="userId">用户代吗</param>
        /// <param name="permissionItemId">权限代吗</param>
        /// <returns>主键数组</returns>
        public string[] GetOrganizeIds(string userId, string permissionItemId)
        {
            string[] returnValue = null;
            string[] names = new string[4];
            string[] values = new string[4];
            names[0] = BaseResourcePermissionScopeTable.FieldPermissionId;
            values[0] = permissionItemId;
            names[1] = BaseResourcePermissionScopeTable.FieldResourceCategory;
            values[1] = "User";
            names[2] = BaseResourcePermissionScopeTable.FieldResourceId;
            values[2] = userId;
            names[3] = BaseResourcePermissionScopeTable.FieldTargetCategory;
            values[3] = "Organize";
            DataTable dataTable = this.GetDT(names, values);
            returnValue = BaseBusinessLogic.FieldToArray(dataTable, BaseResourcePermissionScopeTable.FieldTargetId);
            return returnValue;
        }
        #endregion

        #region public string[] GetRoleIds(string userId, string permissionItemId) 获取职员的角色权限域主键数组
        /// <summary>
        /// 获取职员的角色权限域主键数组
        /// </summary>
        /// <param name="userId">用户代吗</param>
        /// <param name="permissionItemId">权限代吗</param>
        /// <returns>主键数组</returns>
        public string[] GetRoleIds(string userId, string permissionItemId)
        {
            string[] returnValue = null;
            string[] names = new string[4];
            string[] values = new string[4];
            names[0] = BaseResourcePermissionScopeTable.FieldPermissionId;
            values[0] = permissionItemId;
            names[1] = BaseResourcePermissionScopeTable.FieldResourceCategory;
            values[1] = "User";
            names[2] = BaseResourcePermissionScopeTable.FieldResourceId;
            values[2] = userId;
            names[3] = BaseResourcePermissionScopeTable.FieldTargetCategory;
            values[3] = "Role";
            DataTable dataTable = this.GetDT(names, values);
            returnValue = BaseBusinessLogic.FieldToArray(dataTable, BaseResourcePermissionScopeTable.FieldTargetId);
            return returnValue;
        }
        #endregion

        #region public string[] GetUserIds(string userId, string permissionItemId) 获取职员的职员权限域主键数组
        /// <summary>
        /// 获取职员的职员权限域主键数组
        /// </summary>
        /// <param name="userId">用户代吗</param>
        /// <param name="permissionItemId">权限代吗</param>
        /// <returns>主键数组</returns>
        public string[] GetUserIds(string userId, string permissionItemId)
        {
            string[] returnValue = null;
            string[] names = new string[4];
            string[] values = new string[4];
            names[0] = BaseResourcePermissionScopeTable.FieldPermissionId;
            values[0] = permissionItemId;
            names[1] = BaseResourcePermissionScopeTable.FieldResourceCategory;
            values[1] = "User";
            names[2] = BaseResourcePermissionScopeTable.FieldResourceId;
            values[2] = userId;
            names[3] = BaseResourcePermissionScopeTable.FieldTargetCategory;
            values[3] = "User";
            DataTable dataTable = this.GetDT(names, values);
            returnValue = BaseBusinessLogic.FieldToArray(dataTable, BaseResourcePermissionScopeTable.FieldTargetId);
            return returnValue;
        }
        #endregion

        #region public string[] GetModuleIds(string userId, string permissionItemId) 获取职员的模块权限域主键数组
        /// <summary>
        /// 获取职员的职员权限域主键数组
        /// </summary>
        /// <param name="userId">用户代吗</param>
        /// <param name="permissionItemId">权限代吗</param>
        /// <returns>主键数组</returns>
        public string[] GetModuleIds(string userId, string permissionItemId)
        {
            string[] returnValue = null;
            string[] names = new string[4];
            string[] values = new string[4];
            names[0] = BaseResourcePermissionScopeTable.FieldPermissionId;
            values[0] = permissionItemId;
            names[1] = BaseResourcePermissionScopeTable.FieldResourceCategory;
            values[1] = "User";
            names[2] = BaseResourcePermissionScopeTable.FieldResourceId;
            values[2] = userId;
            names[3] = BaseResourcePermissionScopeTable.FieldTargetCategory;
            values[3] = "Module";
            DataTable dataTable = this.GetDT(names, values);
            returnValue = BaseBusinessLogic.FieldToArray(dataTable, BaseResourcePermissionScopeTable.FieldTargetId);
            return returnValue;
        }
        #endregion

        /////////////////////////////////////////////  Organize 部分  //////////////////////////////////////////////////
        
        //
        // 授予权限域的实现部分
        //

        #region private string GrantOrganize(BaseResourcePermissionScopeManager resourcePermissionScopeManager, string id, string userId, string permissionItemId, string organizeId) 为了提高授权的运行速度
        /// <summary>
        /// 为了提高授权的运行速度
        /// </summary>
        /// <param name="resourcePermissionScopeManager">权限域读写器</param>
        /// <param name="Id">主键</param>
        /// <param name="userId">用户主键</param>
        /// <param name="permissionItemId">权限主键</param>
        /// <param name="organizeId">权组织机构限主键</param>
        /// <returns>主键</returns>
        private string GrantOrganize(BaseResourcePermissionScopeManager resourcePermissionScopeManager, string userId, string permissionItemId, string organizeId)
        {
            string returnValue = string.Empty;
            BaseResourcePermissionScopeEntity resourcePermissionScope = new BaseResourcePermissionScopeEntity();
            resourcePermissionScope.PermissionId = int.Parse(permissionItemId);
            resourcePermissionScope.ResourceCategory = "User";
            resourcePermissionScope.ResourceId = userId;
            resourcePermissionScope.TargetCategory = "Organize";
            resourcePermissionScope.TargetId = organizeId;
            resourcePermissionScope.Enabled = 1;
            return resourcePermissionScopeManager.Add(resourcePermissionScope);
        }
        #endregion

        #region public string GrantOrganize(string userId, string permissionItemId, string organizeId) 职员授予权限
        /// <summary>
        /// 职员授予权限
        /// </summary>
        /// <param name="userId">用户主键</param>
        /// <param name="permissionItemId">权限主键</param>
        /// <param name="organizeId">权组织机构限主键</param>
        /// <returns>主键</returns>
        public string GrantOrganize(string userId, string permissionItemId, string organizeId)
        {
            BaseResourcePermissionScopeManager resourcePermissionScopeManager = new BaseResourcePermissionScopeManager(DbHelper, UserInfo);
            return this.GrantOrganize(resourcePermissionScopeManager, userId, permissionItemId, organizeId);
        }
        #endregion

        public int GrantOrganize(string userId, string permissionItemId, string[] organizeIds)
        {
            int returnValue = 0;
            BaseResourcePermissionScopeManager resourcePermissionScopeManager = new BaseResourcePermissionScopeManager(DbHelper, UserInfo);
            for (int i = 0; i < organizeIds.Length; i++)
            {
                this.GrantOrganize(resourcePermissionScopeManager, userId, permissionItemId, organizeIds[i]);
                returnValue++;
            }
            return returnValue;
        }

        public int GrantOrganize(string[] userIds, string permissionItemId, string organizeId)
        {
            int returnValue = 0;
            BaseResourcePermissionScopeManager resourcePermissionScopeManager = new BaseResourcePermissionScopeManager(DbHelper, UserInfo);
            for (int i = 0; i < userIds.Length; i++)
            {
                this.GrantOrganize(resourcePermissionScopeManager, userIds[i], permissionItemId, organizeId);
                returnValue++;
            }
            return returnValue;
        }

        public int GrantOrganize(string[] userIds, string permissionItemId, string[] organizeIds)
        {
            int returnValue = 0;
            BaseResourcePermissionScopeManager resourcePermissionScopeManager = new BaseResourcePermissionScopeManager(DbHelper, UserInfo);
            for (int i = 0; i < userIds.Length; i++)
            {
                for (int j = 0; j < organizeIds.Length; j++)
                {
                    this.GrantOrganize(resourcePermissionScopeManager, userIds[i], permissionItemId, organizeIds[j]);
                    returnValue++;
                }
            }
            return returnValue;
        }
        
        //
        //  撤销权限域的实现部分
        //

        #region private int RevokeOrganize(BaseResourcePermissionScopeManager resourcePermissionScopeManager, string userId, string permissionItemId, string organizeId) 为了提高撤销的运行速度
        /// <summary>
        /// 为了提高撤销的运行速度
        /// </summary>
        /// <param name="resourcePermissionScopeManager">权限域读写器</param>
        /// <param name="userId">用户主键</param>
        /// <param name="permissionItemId">权限主键</param>
        /// <param name="organizeId">权组织机构限主键</param>
        /// <returns>影响行数</returns>
        private int RevokeOrganize(BaseResourcePermissionScopeManager resourcePermissionScopeManager, string userId, string permissionItemId, string organizeId)
        {
            string[] names = new string[4];
            string[] values = new string[4];
            names[0] = BaseResourcePermissionScopeTable.FieldResourceCategory;
            values[0] = "User";
            names[1] = BaseResourcePermissionScopeTable.FieldResourceId;
            values[1] = userId;
            names[2] = BaseResourcePermissionScopeTable.FieldTargetCategory;
            values[2] = "Organize";
            names[3] = BaseResourcePermissionScopeTable.FieldTargetId;
            values[3] = organizeId;
            return resourcePermissionScopeManager.BatchDelete(names, values);
        }
        #endregion

        #region public int RevokeOrganize(string userId, string permissionItemId, string organizeId) 撤销职员权限
        /// <summary>
        /// 撤销职员权限
        /// </summary>
        /// <param name="userId">用户主键</param>
        /// <param name="permissionItemId">权限主键</param>
        /// <param name="organizeId">权组织机构限主键</param>
        /// <returns>影响行数</returns>
        public int RevokeOrganize(string userId, string permissionItemId, string organizeId)
        {
            BaseResourcePermissionScopeManager resourcePermissionScopeManager = new BaseResourcePermissionScopeManager(DbHelper, UserInfo);
            return this.RevokeOrganize(resourcePermissionScopeManager, permissionItemId, userId, organizeId);
        }
        #endregion

        public int RevokeOrganize(string userId, string permissionItemId, string[] organizeIds)
        {
            int returnValue = 0;
            BaseResourcePermissionScopeManager resourcePermissionScopeManager = new BaseResourcePermissionScopeManager(DbHelper, UserInfo);
            for (int i = 0; i < organizeIds.Length; i++)
            {
                returnValue += this.RevokeOrganize(resourcePermissionScopeManager, userId, permissionItemId, organizeIds[i]);
            }
            return returnValue;
        }

        public int RevokeOrganize(string[] userIds, string permissionItemId, string organizeId)
        {
            int returnValue = 0;
            BaseResourcePermissionScopeManager resourcePermissionScopeManager = new BaseResourcePermissionScopeManager(DbHelper, UserInfo);
            for (int i = 0; i < userIds.Length; i++)
            {
                returnValue += this.RevokeOrganize(resourcePermissionScopeManager, userIds[i], permissionItemId, organizeId);
            }
            return returnValue;
        }

        public int RevokeOrganize(string[] userIds, string permissionItemId, string[] organizeIds)
        {
            int returnValue = 0;
            BaseResourcePermissionScopeManager resourcePermissionScopeManager = new BaseResourcePermissionScopeManager(DbHelper, UserInfo);
            for (int i = 0; i < userIds.Length; i++)
            {
                for (int j = 0; j < organizeIds.Length; j++)
                {
                    returnValue += this.RevokeOrganize(resourcePermissionScopeManager, userIds[i], permissionItemId, organizeIds[j]);
                }
            }
            return returnValue;
        }


        /////////////////////////////////////////////  Role 部分  //////////////////////////////////////////////////////

        //
        // 授予权限域的实现部分
        //

        #region private string GrantRole(BaseResourcePermissionScopeManager resourcePermissionScopeManager, string id, string userId, string permissionItemId, string roleId) 为了提高授权的运行速度
        /// <summary>
        /// 为了提高授权的运行速度
        /// </summary>
        /// <param name="resourcePermissionScopeManager">权限域读写器</param>
        /// <param name="Id">主键</param>
        /// <param name="userId">用户主键</param>
        /// <param name="permissionItemId">权限主键</param>
        /// <param name="roleId">权组织机构限主键</param>
        /// <returns>主键</returns>
        private string GrantRole(BaseResourcePermissionScopeManager resourcePermissionScopeManager, string userId, string permissionItemId, string roleId)
        {
            string returnValue = string.Empty;
            BaseResourcePermissionScopeEntity resourcePermissionScope = new BaseResourcePermissionScopeEntity();
            resourcePermissionScope.PermissionId = int.Parse(permissionItemId);
            resourcePermissionScope.ResourceCategory = "User";
            resourcePermissionScope.ResourceId = userId;
            resourcePermissionScope.TargetCategory = "Role";
            resourcePermissionScope.TargetId = roleId;
            resourcePermissionScope.Enabled = 1;
            return resourcePermissionScopeManager.Add(resourcePermissionScope);
        }
        #endregion

        #region public string GrantRole(string userId, string permissionItemId, string roleId) 职员授予权限
        /// <summary>
        /// 职员授予权限
        /// </summary>
        /// <param name="userId">用户主键</param>
        /// <param name="permissionItemId">权限主键</param>
        /// <param name="roleId">权组织机构限主键</param>
        /// <returns>主键</returns>
        public string GrantRole(string userId, string permissionItemId, string roleId)
        {
            BaseResourcePermissionScopeManager resourcePermissionScopeManager = new BaseResourcePermissionScopeManager(DbHelper, UserInfo);
            return this.GrantRole(resourcePermissionScopeManager, userId, permissionItemId, roleId);
        }
        #endregion

        public int GrantRole(string userId, string permissionItemId, string[] roleIds)
        {
            int returnValue = 0;
            BaseResourcePermissionScopeManager resourcePermissionScopeManager = new BaseResourcePermissionScopeManager(DbHelper, UserInfo);
            for (int i = 0; i < roleIds.Length; i++)
            {
                this.GrantRole(resourcePermissionScopeManager, userId, permissionItemId, roleIds[i]);
                returnValue++;
            }
            return returnValue;
        }

        public int GrantRole(string[] userIds, string permissionItemId, string roleId)
        {
            int returnValue = 0;
            BaseResourcePermissionScopeManager resourcePermissionScopeManager = new BaseResourcePermissionScopeManager(DbHelper, UserInfo);
            for (int i = 0; i < userIds.Length; i++)
            {
                this.GrantRole(resourcePermissionScopeManager, userIds[i], permissionItemId, roleId);
                returnValue++;
            }
            return returnValue;
        }

        public int GrantRole(string[] userIds, string permissionItemId, string[] roleIds)
        {
            int returnValue = 0;
            BaseResourcePermissionScopeManager resourcePermissionScopeManager = new BaseResourcePermissionScopeManager(DbHelper, UserInfo);
            for (int i = 0; i < userIds.Length; i++)
            {
                for (int j = 0; j < roleIds.Length; j++)
                {
                    this.GrantRole(resourcePermissionScopeManager, userIds[i], permissionItemId, roleIds[j]);
                    returnValue++;
                }
            }
            return returnValue;
        }
        
        //
        //  撤销权限域的实现部分
        //

        #region private int RevokeRole(BaseResourcePermissionScopeManager resourcePermissionScopeManager, string userId, string permissionItemId, string roleId) 为了提高撤销的运行速度
        /// <summary>
        /// 为了提高撤销的运行速度
        /// </summary>
        /// <param name="resourcePermissionScopeManager">权限域读写器</param>
        /// <param name="userId">用户主键</param>
        /// <param name="permissionItemId">权限主键</param>
        /// <param name="roleId">权组织机构限主键</param>
        /// <returns>影响行数</returns>
        private int RevokeRole(BaseResourcePermissionScopeManager resourcePermissionScopeManager, string userId, string permissionItemId, string roleId)
        {
            string[] names = new string[4];
            string[] values = new string[4];
            names[0] = BaseResourcePermissionScopeTable.FieldResourceCategory;
            values[0] = "User";
            names[1] = BaseResourcePermissionScopeTable.FieldResourceId;
            values[1] = userId;
            names[2] = BaseResourcePermissionScopeTable.FieldTargetCategory;
            values[2] = "Role";
            names[3] = BaseResourcePermissionScopeTable.FieldTargetId;
            values[3] = roleId;
            return resourcePermissionScopeManager.BatchDelete(names, values);
        }
        #endregion

        #region public int RevokeRole(string userId, string permissionItemId, string roleId) 撤销职员权限
        /// <summary>
        /// 撤销职员权限
        /// </summary>
        /// <param name="userId">用户主键</param>
        /// <param name="permissionItemId">权限主键</param>
        /// <param name="roleId">权组织机构限主键</param>
        /// <returns>影响行数</returns>
        public int RevokeRole(string userId, string permissionItemId, string roleId)
        {
            BaseResourcePermissionScopeManager resourcePermissionScopeManager = new BaseResourcePermissionScopeManager(DbHelper, UserInfo);
            return this.RevokeRole(resourcePermissionScopeManager, permissionItemId, userId, roleId);
        }
        #endregion

        public int RevokeRole(string userId, string permissionItemId, string[] roleIds)
        {
            int returnValue = 0;
            BaseResourcePermissionScopeManager resourcePermissionScopeManager = new BaseResourcePermissionScopeManager(DbHelper, UserInfo);
            for (int i = 0; i < roleIds.Length; i++)
            {
                returnValue += this.RevokeRole(resourcePermissionScopeManager, userId, permissionItemId, roleIds[i]);
            }
            return returnValue;
        }

        public int RevokeRole(string[] userIds, string permissionItemId, string roleId)
        {
            int returnValue = 0;
            BaseResourcePermissionScopeManager resourcePermissionScopeManager = new BaseResourcePermissionScopeManager(DbHelper, UserInfo);
            for (int i = 0; i < userIds.Length; i++)
            {
                returnValue += this.RevokeRole(resourcePermissionScopeManager, userIds[i], permissionItemId, roleId);
            }
            return returnValue;
        }

        public int RevokeRole(string[] userIds, string permissionItemId, string[] roleIds)
        {
            int returnValue = 0;
            BaseResourcePermissionScopeManager resourcePermissionScopeManager = new BaseResourcePermissionScopeManager(DbHelper, UserInfo);
            for (int i = 0; i < userIds.Length; i++)
            {
                for (int j = 0; j < roleIds.Length; j++)
                {
                    returnValue += this.RevokeRole(resourcePermissionScopeManager, userIds[i], permissionItemId, roleIds[j]);
                }
            }
            return returnValue;
        }


        /////////////////////////////////////////////  User 部分  /////////////////////////////////////////////////////

        //
        // 授予权限域的实现部分
        //

        #region private string GrantUser(BaseResourcePermissionScopeManager resourcePermissionScopeManager, string id, string userId, string permissionItemId, string targetUserId) 为了提高授权的运行速度
        /// <summary>
        /// 为了提高授权的运行速度
        /// </summary>
        /// <param name="resourcePermissionScopeManager">权限域读写器</param>
        /// <param name="Id">主键</param>
        /// <param name="userId">用户主键</param>
        /// <param name="permissionItemId">权限主键</param>
        /// <param name="targetUserId">职员主键</param>
        /// <returns>主键</returns>
        private string GrantUser(BaseResourcePermissionScopeManager resourcePermissionScopeManager, string id, string userId, string permissionItemId, string targetUserId)
        {
            string returnValue = string.Empty;
            BaseResourcePermissionScopeEntity resourcePermissionScope = new BaseResourcePermissionScopeEntity();
            resourcePermissionScope.PermissionId = int.Parse(permissionItemId);
            resourcePermissionScope.ResourceCategory = "User";
            resourcePermissionScope.ResourceId = userId;
            resourcePermissionScope.TargetCategory = "User";
            resourcePermissionScope.TargetId = targetUserId;
            resourcePermissionScope.Enabled = 1;
            return resourcePermissionScopeManager.Add(resourcePermissionScope);
        }
        #endregion

        #region public string GrantUser(string userId, string permissionItemId, string targetUserId) 职员授予权限
        /// <summary>
        /// 职员授予权限
        /// </summary>
        /// <param name="userId">用户主键</param>
        /// <param name="permissionItemId">权限主键</param>
        /// <param name="targetUserId">职员主键</param>
        /// <returns>主键</returns>
        public string GrantUser(string userId, string permissionItemId, string targetUserId)
        {
            BaseResourcePermissionScopeManager resourcePermissionScopeManager = new BaseResourcePermissionScopeManager(DbHelper, UserInfo);
            return this.GrantUser(resourcePermissionScopeManager, string.Empty, userId, permissionItemId, targetUserId);
        }
        #endregion

        public int GrantUser(string userId, string permissionItemId, string[] targetUserIds)
        {
            int returnValue = 0;
            BaseSequenceManager sequenceManager = new BaseSequenceManager(DbHelper);
            string[] sequenceIds = sequenceManager.GetBatchSequence(BaseResourcePermissionTable.TableName, targetUserIds.Length);
            BaseResourcePermissionScopeManager resourcePermissionScopeManager = new BaseResourcePermissionScopeManager(DbHelper, UserInfo);
            for (int i = 0; i < targetUserIds.Length; i++)
            {
                this.GrantUser(resourcePermissionScopeManager, sequenceIds[i], userId, permissionItemId, targetUserIds[i]);
                returnValue++;
            }
            return returnValue;
        }

        public int GrantUser(string[] userIds, string permissionItemId, string targetUserId)
        {
            int returnValue = 0;
            BaseSequenceManager sequenceManager = new BaseSequenceManager(DbHelper);
            string[] sequenceIds = sequenceManager.GetBatchSequence(BaseResourcePermissionTable.TableName, userIds.Length);
            BaseResourcePermissionScopeManager resourcePermissionScopeManager = new BaseResourcePermissionScopeManager(DbHelper, UserInfo);
            for (int i = 0; i < userIds.Length; i++)
            {
                this.GrantUser(resourcePermissionScopeManager, sequenceIds[i], userIds[i], permissionItemId, targetUserId);
                returnValue++;
            }
            return returnValue;
        }

        public int GrantUser(string[] userIds, string permissionItemId, string[] targetUserIds)
        {
            int returnValue = 0;
            BaseSequenceManager sequenceManager = new BaseSequenceManager(DbHelper);
            string[] sequenceIds = sequenceManager.GetBatchSequence(BaseResourcePermissionTable.TableName, userIds.Length * targetUserIds.Length);
            BaseResourcePermissionScopeManager resourcePermissionScopeManager = new BaseResourcePermissionScopeManager(DbHelper, UserInfo);
            for (int i = 0; i < userIds.Length; i++)
            {
                for (int j = 0; j < targetUserIds.Length; j++)
                {
                    this.GrantUser(resourcePermissionScopeManager, sequenceIds[i * userIds.Length + j], userIds[i], permissionItemId, targetUserIds[j]);
                    returnValue++;
                }
            }
            return returnValue;
        }

        //
        //  撤销权限域的实现部分
        //

        #region private int RevokeUser(BaseResourcePermissionScopeManager resourcePermissionScopeManager, string userId, string permissionItemId, string targetUserId) 为了提高撤销的运行速度
        /// <summary>
        /// 为了提高撤销的运行速度
        /// </summary>
        /// <param name="resourcePermissionScopeManager">权限域读写器</param>
        /// <param name="userId">用户主键</param>
        /// <param name="permissionItemId">权限主键</param>
        /// <param name="targetUserId">职员主键</param>
        /// <returns>影响行数</returns>
        private int RevokeUser(BaseResourcePermissionScopeManager resourcePermissionScopeManager, string userId, string permissionItemId, string targetUserId)
        {
            string[] names = new string[4];
            string[] values = new string[4];
            names[0] = BaseResourcePermissionScopeTable.FieldResourceCategory;
            values[0] = "User";
            names[1] = BaseResourcePermissionScopeTable.FieldResourceId;
            values[1] = userId;
            names[2] = BaseResourcePermissionScopeTable.FieldTargetCategory;
            values[2] = "User";
            names[3] = BaseResourcePermissionScopeTable.FieldTargetId;
            values[3] = targetUserId;
            return resourcePermissionScopeManager.BatchDelete(names, values);
        }
        #endregion

        #region public int RevokeUser(string userId, string permissionItemId, string targetUserId) 撤销职员权限
        /// <summary>
        /// 撤销职员权限
        /// </summary>
        /// <param name="userId">用户主键</param>
        /// <param name="permissionItemId">权限主键</param>
        /// <param name="targetUserId">职员主键</param>
        /// <returns>影响行数</returns>
        public int RevokeUser(string userId, string permissionItemId, string targetUserId)
        {
            BaseResourcePermissionScopeManager resourcePermissionScopeManager = new BaseResourcePermissionScopeManager(DbHelper, UserInfo);
            return this.RevokeUser(resourcePermissionScopeManager, permissionItemId, userId, targetUserId);
        }
        #endregion

        public int RevokeUser(string userId, string permissionItemId, string[] targetUserIds)
        {
            int returnValue = 0;
            BaseResourcePermissionScopeManager resourcePermissionScopeManager = new BaseResourcePermissionScopeManager(DbHelper, UserInfo);
            for (int i = 0; i < targetUserIds.Length; i++)
            {
                returnValue += this.RevokeUser(resourcePermissionScopeManager, userId, permissionItemId, targetUserIds[i]);
            }
            return returnValue;
        }

        public int RevokeUser(string[] userIds, string permissionItemId, string targetUserId)
        {
            int returnValue = 0;
            BaseResourcePermissionScopeManager resourcePermissionScopeManager = new BaseResourcePermissionScopeManager(DbHelper, UserInfo);
            for (int i = 0; i < userIds.Length; i++)
            {
                returnValue += this.RevokeUser(resourcePermissionScopeManager, userIds[i], permissionItemId, targetUserId);
            }
            return returnValue;
        }

        public int RevokeUser(string[] userIds, string permissionItemId, string[] targetUserIds)
        {
            int returnValue = 0;
            BaseResourcePermissionScopeManager resourcePermissionScopeManager = new BaseResourcePermissionScopeManager(DbHelper, UserInfo);
            for (int i = 0; i < userIds.Length; i++)
            {
                for (int j = 0; j < targetUserIds.Length; j++)
                {
                    returnValue += this.RevokeUser(resourcePermissionScopeManager, userIds[i], permissionItemId, targetUserIds[j]);
                }
            }
            return returnValue;
        }


        /////////////////////////////////////////////  Module 部分  /////////////////////////////////////////////////////

        //
        // 授予权限域的实现部分
        //

        #region private string GrantModule(BaseResourcePermissionScopeManager resourcePermissionScopeManager, string id, string userId, string permissionItemId, string targetModuleId) 为了提高授权的运行速度
        /// <summary>
        /// 为了提高授权的运行速度
        /// </summary>
        /// <param name="resourcePermissionScopeManager">权限域读写器</param>
        /// <param name="Id">主键</param>
        /// <param name="userId">用户主键</param>
        /// <param name="permissionItemId">权限主键</param>
        /// <param name="targetModuleId">职员主键</param>
        /// <returns>主键</returns>
        private string GrantModule(BaseResourcePermissionScopeManager resourcePermissionScopeManager, string userId, string permissionItemId, string targetModuleId)
        {
            string returnValue = string.Empty;
            BaseResourcePermissionScopeEntity resourcePermissionScope = new BaseResourcePermissionScopeEntity();
            resourcePermissionScope.PermissionId = int.Parse(permissionItemId);
            resourcePermissionScope.ResourceCategory = "User";
            resourcePermissionScope.ResourceId = userId;
            resourcePermissionScope.TargetCategory = "Module";
            resourcePermissionScope.TargetId = targetModuleId;
            resourcePermissionScope.Enabled = 1;
            return resourcePermissionScopeManager.Add(resourcePermissionScope);
        }
        #endregion

        #region public string GrantModule(string userId, string permissionItemId, string targetModuleId) 职员授予权限
        /// <summary>
        /// 职员授予权限
        /// </summary>
        /// <param name="userId">用户主键</param>
        /// <param name="permissionItemId">权限主键</param>
        /// <param name="targetModuleId">职员主键</param>
        /// <returns>主键</returns>
        public string GrantModule(string userId, string permissionItemId, string targetModuleId)
        {
            BaseResourcePermissionScopeManager resourcePermissionScopeManager = new BaseResourcePermissionScopeManager(DbHelper, UserInfo);
            return this.GrantModule(resourcePermissionScopeManager, userId, permissionItemId, targetModuleId);
        }
        #endregion

        public int GrantModule(string userId, string permissionItemId, string[] targetModuleIds)
        {
            int returnValue = 0;
            BaseResourcePermissionScopeManager resourcePermissionScopeManager = new BaseResourcePermissionScopeManager(DbHelper, UserInfo);
            for (int i = 0; i < targetModuleIds.Length; i++)
            {
                this.GrantModule(resourcePermissionScopeManager, userId, permissionItemId, targetModuleIds[i]);
                returnValue++;
            }
            return returnValue;
        }

        public int GrantModule(string[] userIds, string permissionItemId, string targetModuleId)
        {
            int returnValue = 0;
            BaseResourcePermissionScopeManager resourcePermissionScopeManager = new BaseResourcePermissionScopeManager(DbHelper, UserInfo);
            for (int i = 0; i < userIds.Length; i++)
            {
                this.GrantModule(resourcePermissionScopeManager, userIds[i], permissionItemId, targetModuleId);
                returnValue++;
            }
            return returnValue;
        }

        public int GrantModule(string[] userIds, string permissionItemId, string[] targetModuleIds)
        {
            int returnValue = 0;
            BaseResourcePermissionScopeManager resourcePermissionScopeManager = new BaseResourcePermissionScopeManager(DbHelper, UserInfo);
            for (int i = 0; i < userIds.Length; i++)
            {
                for (int j = 0; j < targetModuleIds.Length; j++)
                {
                    this.GrantModule(resourcePermissionScopeManager, userIds[i], permissionItemId, targetModuleIds[j]);
                    returnValue++;
                }
            }
            return returnValue;
        }

        //
        //  撤销权限域的实现部分
        //

        #region private int RevokeModule(BaseResourcePermissionScopeManager resourcePermissionScopeManager, string userId, string permissionItemId, string targetModuleId) 为了提高撤销的运行速度
        /// <summary>
        /// 为了提高撤销的运行速度
        /// </summary>
        /// <param name="resourcePermissionScopeManager">权限域读写器</param>
        /// <param name="userId">用户主键</param>
        /// <param name="permissionItemId">权限主键</param>
        /// <param name="targetModuleId">职员主键</param>
        /// <returns>影响行数</returns>
        private int RevokeModule(BaseResourcePermissionScopeManager resourcePermissionScopeManager, string userId, string permissionItemId, string targetModuleId)
        {
            string[] names = new string[4];
            string[] values = new string[4];
            names[0] = BaseResourcePermissionScopeTable.FieldResourceCategory;
            values[0] = "User";
            names[1] = BaseResourcePermissionScopeTable.FieldResourceId;
            values[1] = userId;
            names[2] = BaseResourcePermissionScopeTable.FieldTargetCategory;
            values[2] = "Module";
            names[3] = BaseResourcePermissionScopeTable.FieldTargetId;
            values[3] = targetModuleId;
            return resourcePermissionScopeManager.BatchDelete(names, values);
        }
        #endregion

        #region public int RevokeModule(string userId, string permissionItemId, string targetModuleId) 撤销职员权限
        /// <summary>
        /// 撤销职员权限
        /// </summary>
        /// <param name="userId">用户主键</param>
        /// <param name="permissionItemId">权限主键</param>
        /// <param name="targetModuleId">职员主键</param>
        /// <returns>影响行数</returns>
        public int RevokeModule(string userId, string permissionItemId, string targetModuleId)
        {
            BaseResourcePermissionScopeManager resourcePermissionScopeManager = new BaseResourcePermissionScopeManager(DbHelper, UserInfo);
            return this.RevokeModule(resourcePermissionScopeManager, permissionItemId, userId, targetModuleId);
        }
        #endregion

        public int RevokeModule(string userId, string permissionItemId, string[] targetModuleIds)
        {
            int returnValue = 0;
            BaseResourcePermissionScopeManager resourcePermissionScopeManager = new BaseResourcePermissionScopeManager(DbHelper, UserInfo);
            for (int i = 0; i < targetModuleIds.Length; i++)
            {
                returnValue += this.RevokeModule(resourcePermissionScopeManager, userId, permissionItemId, targetModuleIds[i]);
            }
            return returnValue;
        }

        public int RevokeModule(string[] userIds, string permissionItemId, string targetModuleId)
        {
            int returnValue = 0;
            BaseResourcePermissionScopeManager resourcePermissionScopeManager = new BaseResourcePermissionScopeManager(DbHelper, UserInfo);
            for (int i = 0; i < userIds.Length; i++)
            {
                returnValue += this.RevokeModule(resourcePermissionScopeManager, userIds[i], permissionItemId, targetModuleId);
            }
            return returnValue;
        }

        public int RevokeModule(string[] userIds, string permissionItemId, string[] targetModuleIds)
        {
            int returnValue = 0;
            BaseResourcePermissionScopeManager resourcePermissionScopeManager = new BaseResourcePermissionScopeManager(DbHelper, UserInfo);
            for (int i = 0; i < userIds.Length; i++)
            {
                for (int j = 0; j < targetModuleIds.Length; j++)
                {
                    returnValue += this.RevokeModule(resourcePermissionScopeManager, userIds[i], permissionItemId, targetModuleIds[j]);
                }
            }
            return returnValue;
        }



        /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        ////                                                                                                         ////
        ////                                            授权范围管理部分                                             ////
        ////                                                                                                         ////
        /////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        #region public string[] GetLicensePermissionIds(string userId) 获取职员的权限主键数组
        /// <summary>
        /// 获取职员的权限主键数组
        /// </summary>
        /// <param name="userId">用户代吗</param>
        /// <returns>主键数组</returns>
        public string[] GetLicensePermissionIds(string userId)
        {
            string[] returnValue = null;
            string[] names = new string[3];
            string[] values = new string[3];
            names[0] = BaseResourcePermissionScopeTable.FieldResourceCategory;
            values[0] = "User";
            names[1] = BaseResourcePermissionScopeTable.FieldResourceId;
            values[1] = userId;
            names[2] = BaseResourcePermissionScopeTable.FieldTargetCategory;
            values[2] = "Permission";
            DataTable dataTable = this.GetDT(names, values);
            returnValue = BaseBusinessLogic.FieldToArray(dataTable, BaseResourcePermissionScopeTable.FieldTargetId);
            return returnValue;
        }
        #endregion

        //
        // 授予授权范围的实现部分
        //

        #region private string GrantLicense(BaseResourcePermissionScopeManager resourcePermissionScopeManager, string id, string userId, string permissionItemId) 为了提高授权的运行速度
        /// <summary>
        /// 为了提高授权的运行速度
        /// </summary>
        /// <param name="resourcePermissionScopeManager">权限域读写器</param>
        /// <param name="Id">主键</param>
        /// <param name="userId">用户主键</param>
        /// <param name="permissionItemId">权限主键</param>
        /// <returns>主键</returns>
        private string GrantLicense(BaseResourcePermissionScopeManager resourcePermissionScopeManager, string userId, string permissionItemId)
        {
            string returnValue = string.Empty;
            BasePermissionItemManager permissionItemManager = new BasePermissionItemManager(DbHelper);
            string licensePermissionId = permissionItemManager.GetID(BasePermissionItemTable.FieldCode, "LicensePermission");
            BaseResourcePermissionScopeEntity resourcePermissionScope = new BaseResourcePermissionScopeEntity();
            resourcePermissionScope.PermissionId = int.Parse(licensePermissionId);
            resourcePermissionScope.ResourceCategory = "User";
            resourcePermissionScope.ResourceId = userId;
            resourcePermissionScope.TargetCategory = "Permission";
            resourcePermissionScope.TargetId = permissionItemId;
            resourcePermissionScope.Enabled = 1;
            return resourcePermissionScopeManager.Add(resourcePermissionScope);
        }
        #endregion

        #region public string GrantLicense(string userId, string permissionItemId) 职员授予权限
        /// <summary>
        /// 职员授予权限
        /// </summary>
        /// <param name="userId">用户主键</param>
        /// <param name="permissionItemId">权限主键</param>
        /// <param name="organizeId">权组织机构限主键</param>
        /// <returns>主键</returns>
        public string GrantLicense(string userId, string permissionItemId)
        {
            BaseResourcePermissionScopeManager resourcePermissionScopeManager = new BaseResourcePermissionScopeManager(DbHelper, UserInfo);
            return this.GrantLicense(resourcePermissionScopeManager, userId, permissionItemId);
        }
        #endregion

        public int GrantLicense(string userId, string[] permissionItemIds)
        {
            int returnValue = 0;
            BaseResourcePermissionScopeManager resourcePermissionScopeManager = new BaseResourcePermissionScopeManager(DbHelper, UserInfo);
            for (int i = 0; i < permissionItemIds.Length; i++)
            {
                this.GrantLicense(resourcePermissionScopeManager, userId, permissionItemIds[i]);
                returnValue++;
            }
            return returnValue;
        }

        public int GrantLicense(string[] userIds, string permissionItemId)
        {
            int returnValue = 0;
            BaseResourcePermissionScopeManager resourcePermissionScopeManager = new BaseResourcePermissionScopeManager(DbHelper, UserInfo);
            for (int i = 0; i < userIds.Length; i++)
            {
                this.GrantLicense(resourcePermissionScopeManager, userIds[i], permissionItemId);
                returnValue++;
            }
            return returnValue;
        }

        public int GrantLicense(string[] userIds, string[] permissionItemIds)
        {
            int returnValue = 0;
            BaseResourcePermissionScopeManager resourcePermissionScopeManager = new BaseResourcePermissionScopeManager(DbHelper, UserInfo);
            for (int i = 0; i < userIds.Length; i++)
            {
                for (int j = 0; j < permissionItemIds.Length; j++)
                {
                    this.GrantLicense(resourcePermissionScopeManager, userIds[i], permissionItemIds[j]);
                    returnValue++;
                }
            }
            return returnValue;
        }


        //
        //  撤销授权范围的实现部分
        //

        #region private int GrantLicense(BaseResourcePermissionScopeManager resourcePermissionScopeManager, string userId, string permissionItemId) 为了提高授权的运行速度
        /// <summary>
        /// 为了提高授权的运行速度
        /// </summary>
        /// <param name="resourcePermissionScopeManager">权限域读写器</param>
        /// <param name="userId">用户主键</param>
        /// <param name="permissionItemId">权限主键</param>
        /// <returns>主键</returns>
        private int RevokeLicense(BaseResourcePermissionScopeManager resourcePermissionScopeManager, string userId, string permissionItemId)
        {
            string[] names = new string[4];
            string[] values = new string[4];
            names[0] = BaseResourcePermissionScopeTable.FieldResourceCategory;
            values[0] = "User";
            names[1] = BaseResourcePermissionScopeTable.FieldResourceId;
            values[1] = userId;
            names[2] = BaseResourcePermissionScopeTable.FieldTargetCategory;
            values[2] = "Permission";
            names[3] = BaseResourcePermissionScopeTable.FieldTargetId;
            values[3] = permissionItemId;
            return resourcePermissionScopeManager.BatchDelete(names, values);
        }
        #endregion

        #region public int GrantLicense(string userId, string permissionItemId) 职员撤销授权
        /// <summary>
        /// 职员撤销授权
        /// </summary>
        /// <param name="userId">用户主键</param>
        /// <param name="permissionItemId">权限主键</param>
        /// <returns>主键</returns>
        public int RevokeLicense(string userId, string permissionItemId)
        {
            BaseResourcePermissionScopeManager resourcePermissionScopeManager = new BaseResourcePermissionScopeManager(DbHelper, UserInfo);
            return this.RevokeLicense(resourcePermissionScopeManager, userId, permissionItemId);
        }
        #endregion

        public int RevokeLicense(string userId, string[] permissionItemIds)
        {
            int returnValue = 0;
            BaseResourcePermissionScopeManager resourcePermissionScopeManager = new BaseResourcePermissionScopeManager(DbHelper, UserInfo);
            for (int i = 0; i < permissionItemIds.Length; i++)
            {
                this.RevokeLicense(resourcePermissionScopeManager, userId, permissionItemIds[i]);
                returnValue++;
            }
            return returnValue;
        }

        public int RevokeLicense(string[] userIds, string permissionItemId)
        {
            int returnValue = 0;
            BaseResourcePermissionScopeManager resourcePermissionScopeManager = new BaseResourcePermissionScopeManager(DbHelper, UserInfo);
            for (int i = 0; i < userIds.Length; i++)
            {
                this.RevokeLicense(resourcePermissionScopeManager, userIds[i], permissionItemId);
                returnValue++;
            }
            return returnValue;
        }

        public int RevokeLicense(string[] userIds, string[] permissionItemIds)
        {
            int returnValue = 0;
            BaseResourcePermissionScopeManager resourcePermissionScopeManager = new BaseResourcePermissionScopeManager(DbHelper, UserInfo);
            for (int i = 0; i < userIds.Length; i++)
            {
                for (int j = 0; j < permissionItemIds.Length; j++)
                {
                    this.RevokeLicense(resourcePermissionScopeManager, userIds[i], permissionItemIds[j]);
                    returnValue++;
                }
            }
            return returnValue;
        }
    }
}